package main

import (
	"fmt"
	"math/rand"
	"strconv"
	"time"

	"github.com/nats-io/nats.go"
)

func randString() string {
	source := rand.NewSource(time.Now().Unix())
	r := rand.New(source)
	return strconv.Itoa(r.Intn(10000))

}
func main() {

	nc, _ := nats.Connect(nats.DefaultURL)

	js, _ := nc.JetStream()

	js.AddStream(&nats.StreamConfig{
		Name:     randString(),
		Subjects: []string{"ORDERS.*"},
	})

	js.Subscribe("ORDERS.scratch", func(m *nats.Msg) {
		fmt.Printf("ORDERS.scratch: %s\n", string(m.Data))
	}, nats.DeliverLast())

	// Subscribe to a wildcard not work.
	js.Subscribe("ORDERS.*", func(m *nats.Msg) {
		fmt.Printf("ORDERS.*: %s\n", string(m.Data))
	}, nats.DeliverLast())

	js.Publish("ORDERS.scratch", []byte("hello"))

	fmt.Println("wait......")
	select {}
}
